#!/usr/bin/python
import zipfile
import glob
import sys
import subprocess
import os

for zip_file in glob.glob('*.zip'):
    zip_ref = zipfile.ZipFile(zip_file, 'r')
    zip_ref.extractall('.')
    zip_ref.close()

for csv_file in glob.glob('API*.csv'):
    fin = open(csv_file, 'r')
    data = fin.read().splitlines(True)
    fout = open('trimmed_' + csv_file, 'w')
    fout.writelines(data[4:])
    fin.close()
    fout.close()
    if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
        subprocess.call(["mongoimport", "-d", "tut", "-c", "wb", "--type", "csv", "--headerline", "trimmed_" + csv_file])
    elif sys.platform.startswith('win'):
        subprocess.call(["mongoimport", "/d", "tut", "/c", "wb", "/type", "csv", "/headerline", "trimmed_" + csv_file])
    else:
        print("unsupported platform")

for csv_file in glob.glob('*.csv'):
    os.remove(csv_file)
